﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XEvents.Addin
{
    public partial class ColumnSelectorForm : Form
    {

        DataGridView gridview;
        public ColumnSelectorForm(DataGridView dgv)
        {
            InitializeComponent();

            gridview = dgv;
            foreach (DataGridViewColumn c in dgv.Columns)
            {
                if (c.Visible)
                    listBox2.Items.Add(c.Name);
                else
                    listBox1.Items.Add(c.Name);
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            gridview.Refresh();
            this.Close();
        }

        private void btn_AddOne_Click(object sender, EventArgs e)
        {
            MoveOneItem(listBox1, listBox2, true);
        }

        private void btn_RemoveOne_Click(object sender, EventArgs e)
        {
            MoveOneItem(listBox2, listBox1, false);
        }

        private void btn_AddAll_Click(object sender, EventArgs e)
        {
            MoveAllItems(listBox1, listBox2, true);
        }

        private void btn_RemoveAll_Click(object sender, EventArgs e)
        {
            MoveAllItems(listBox2, listBox1, false);
        }
        private void MoveOneItem(ListBox source, ListBox destination, bool visible)
        {
            if (source.SelectedItems.Count > 0)
            {
                string[] selection = new string[source.SelectedItems.Count];
                source.SelectedItems.CopyTo(selection, 0);

                foreach (string s in selection)
                {
                    source.Items.Remove(s);
                    destination.Items.Add(s);

                    foreach (DataGridViewColumn c in gridview.Columns)
                    {
                        if (c.Name == s)
                        {
                            c.Visible = visible;
                        }
                    }
                }
            }
        }

        private void MoveAllItems(ListBox source, ListBox destination, bool visible)
        {
            if (source.Items.Count > 0)
            {
                string[] selection = new string[source.Items.Count];
                source.Items.CopyTo(selection, 0);

                foreach (string s in selection)
                {
                    source.Items.Remove(s);
                    destination.Items.Add(s);

                    foreach (DataGridViewColumn c in gridview.Columns)
                    {
                        if (c.Name == s)
                        {
                            c.Visible = visible;
                        }
                    }
                }
            }
        }

        private void listBox2_DoubleClick(object sender, EventArgs e)
        {
            MoveOneItem(listBox2, listBox1, false);
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            MoveOneItem(listBox1, listBox2, true);
        }
    }
}
